using System;
using System.Collections.Generic;
using System.Text;

namespace PastCrew.Games.Chess.Engine.Entities.Pieces
{
    public sealed class Rook : BasePiece
    {

        #region constructors

        /// <summary>
        /// 
        /// </summary>
        public Rook()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pieceColor"></param>
        public Rook(Color pieceColor)
        {
            base.Color = pieceColor;
        }

        #endregion

        #region public methods

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public sealed override List<Entities.SquareStatus> GetValidMoves()
        {
            #region rook rule set

            Board.Position currentPosition = this.CurrentSquare.Position;
            List<Entities.SquareStatus> validPositions = new List<Entities.SquareStatus>();

            #endregion

            // Rook moves right
            if (currentPosition.X < 7)
            {
                for (int x = (currentPosition.X + 1); x <= 7; x++)
                {
                    Entities.Board.Square square = Board.GetSquare(new Board.Position(x, currentPosition.Y));
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }
            // Rook moves left
            if (currentPosition.X > 0)
            {
                for (int x = (currentPosition.X - 1); x >= 0; x--)
                {
                    Entities.Board.Square square = Board.GetSquare(new Board.Position(x, currentPosition.Y));
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }
            // Rook moves up
            if (currentPosition.Y < 7)
            {
                for (int y = (currentPosition.Y + 1); y <= 7; y++)
                {
                    Entities.Board.Square square = Board.GetSquare(new Board.Position(currentPosition.X, y));
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }
            // Rook moves down
            if (currentPosition.Y < 7)
            {
                for (int y = (currentPosition.Y - 1); y >= 0; y--)
                {
                    Entities.Board.Square square = Board.GetSquare(new Board.Position(currentPosition.X, y));
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }

            return validPositions;
        }

        #endregion
    }
}
